/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jack.lib.core.utils;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/9/6 11:22
 */

public class SuperTimer {

    private final long mMillisInFuture;
    private long mCountdownInterval;
    private long mStopTimeInFuture;

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public boolean isEnable(){
        return enable;
    }

    private boolean enable = true;

    private boolean mCancelled = false;


    /**
     *
     * @param time 剩余时间
     * @param interval 循环间隔
     */
    public SuperTimer(long time, long interval) {
        mMillisInFuture = time;
        mCountdownInterval = interval;
    }

    /**
     * 设置时间间隔
     * @param countDownInterval
     */
    public void setCountDownInterval(long countDownInterval){
        mCountdownInterval = countDownInterval;
    }

    /**
     * Cancel the countdown.
     */
    public synchronized final void cancel() {
        mCancelled = true;
        mHandler.removeMessages(MSG);
    }

    public synchronized final SuperTimer start() {
        mCancelled = false;
        if (mMillisInFuture <= 0) {
            onFinish();
            return this;
        }
        mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG), mCountdownInterval);
        return this;
    }


    public void onTick(long millisUntilFinished){

    }
    public void onFinish(){

    }


    private static final int MSG = 1;


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            synchronized (SuperTimer.this) {
                if (mCancelled) {
                    return;
                }

                final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();

                if (millisLeft <= 0) {
                    onFinish();

                } else {
                    long lastTickStart = SystemClock.elapsedRealtime();
                    if (enable){
                        onTick(millisLeft);
                    }

                    long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();

                    while (delay < 0) delay += mCountdownInterval;

                    sendMessageDelayed(obtainMessage(MSG), delay);
                }
            }
        }
    };
}
